﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel.Description;
using System.Xml;

namespace Salient.ServiceModel
{
    /// <summary>
    /// </summary>
    public class PreserveReferencesOperationBehavior :
        DataContractSerializerOperationBehavior
    {
        #region Ctor

        public PreserveReferencesOperationBehavior(
            OperationDescription operationDescription)
            : base(operationDescription)
        {
        }

        #endregion

        #region Public Methods

        public override XmlObjectSerializer CreateSerializer(Type type, string name,
                                                             string ns, IList<Type> knownTypes)
        {
            return CreateDataContractSerializer(type, name, ns, knownTypes);
        }

        public override XmlObjectSerializer CreateSerializer(Type type,
                                                             XmlDictionaryString name, XmlDictionaryString ns,
                                                             IList<Type> knownTypes)
        {
            return new DataContractSerializer(type, name, ns, knownTypes,
                                              2147483646 /*maxItemsInObjectGraph*/,
                                              false /*ignoreExtensionDataObject*/,
                                              true /*preserveObjectReferences*/,
                                              null /*dataContractSurrogate*/);
        }

        #endregion

        #region Private Methods

        private static XmlObjectSerializer CreateDataContractSerializer(Type type,
                                                                        string name, string ns, IList<Type> knownTypes)
        {
            return CreateDataContractSerializer(type, name, ns, knownTypes);
        }

        #endregion
    }
}